package com.teyao.teyao_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teyao.teyao_back.config.myPage;
import com.teyao.teyao_back.entity.CarDeliverWrapper;
import com.teyao.teyao_back.entity.ResponseMessage;

import com.teyao.teyao_back.mapper.CarDeliverMapper;

import com.teyao.teyao_back.service.CarDeliverService;

import org.springframework.stereotype.Service;

@Service
public class CarDeliverImpl extends ServiceImpl<CarDeliverMapper, CarDeliverWrapper> implements CarDeliverService {

    @Override
    public ResponseMessage selectAll() {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(baseMapper.selectList(null));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectByStatus(String status) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectByDriver(String driver) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_name", driver);
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectByNumber(String number) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", number);
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectByOrderId(Integer OrderId) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", OrderId);
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectDriving() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "配送中");
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectNotStart() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未配送");
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public myPage myQuery(Integer currentPage, Integer size, CarDeliverWrapper carDeliverWrapper) {

        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        if (carDeliverWrapper.getOrderId() != null && !carDeliverWrapper.getOrderId().isBlank()) {
            queryWrapper.like("order_id", carDeliverWrapper.getOrderId().trim());
        }
        if (carDeliverWrapper.getStatus() != null && !carDeliverWrapper.getStatus().isBlank()) {
            queryWrapper.eq("status", carDeliverWrapper.getStatus());
        }
        if (carDeliverWrapper.getDriverName() != null && !carDeliverWrapper.getDriverName().isBlank()) {
            queryWrapper.eq("driver_name", carDeliverWrapper.getDriverName());
        }
        if (carDeliverWrapper.getNumber() != null && !carDeliverWrapper.getNumber().isBlank()) {
            queryWrapper.eq("number", carDeliverWrapper.getNumber());
        }
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<CarDeliverWrapper> page = new Page<>(currentPage, size);
        IPage result = baseMapper.selectPage(page, queryWrapper);
        myPage.setData(result.getRecords());
        myPage.setTotal((int) result.getTotal());
        return myPage;
    }
}
